<?php
namespace pfp\lib {
    
    \defined('_JEXEC') or die('Restricted access');

    /**
     * Description of eventManager
     *
     * @todo 
     * 
     * @author George Zeakis<zeageorge@google.com>
     * 
     */
    class eventManager {
        /**
         *
         * @var eventManager
         */
        protected static $_instance = \NULL;

        /**
         *
         * @var pfp
         */
        protected $_parent = \NULL;

        /**
         *
         * @var array
         */
        protected $_registered_events = array();

        /**
         *
         * @var array
         */
        protected $_registered_actions = array();

        /**
         * 
         * @param pfp $parent
         * @return eventManager
         */
        public static function Init(pfp $parent){
            if( self::$_instance === \NULL ) { 
                self::$_instance = new self();
                self::$_instance->_parent = $parent;
                self::$_instance->_registered_events = array();
                self::$_instance->_registered_actions = array();
            }

            return self::$_instance;
        }

        public static function registerEvents(array $events){

        }

        public static function registerEvent($eventName){

        }

        public static function removeEvent($eventName){

        }

        public static function removeEvents(array $events){

        }

        public static function getEvents(){

        }

        /**
         * 
         * @param mix &$eventData
         * @return boolean
         */
        public static function riseEvent(&$eventData){

            if (\version_compare(\PHP_VERSION, '5.4.0') >= 0){ $dt=\debug_backtrace(0,2);} else { $dt=\debug_backtrace(\FALSE); }

            // todo: record the error.
            if (\count($dt)<2){ return \FALSE; }

            //echo '<pre>';
            //var_dump($dt[0]['file']);     // file of the caller
            //var_dump($dt[1]['class']);    // class of the caller
            //var_dump($dt[1]['function']); // function of the caller
            //echo "<br />\n";
            //echo "from eventManager:<br />\n";
            //var_dump($dt[0]['args']);     // riseEvents args ByRef !!!!
            //var_dump($dt[0]['args'][0]['args']);

            $event_data =&$eventData;

            if (!\is_array($event_data)){ return \FALSE; } 
            if (!\array_key_exists("event_name", $event_data)){ return \FALSE;}


    //        $data2Display = "exec flow"."<br />\n";
    //        $data2Display += "file: ".isset($dt[0]['file']) ? $dt[0]['file'] : "unknown, ";
    //        $data2Display += "class: ".isset($dt[1]['class']) ? $dt[1]['class'] : "unknown, ";
    //        $data2Display += "function: ".isset($dt[1]['function']) ? $dt[1]['function'] : "unknown, ";
    //        $data2Display += "event: ".isset($event_data["event_name"]) ? $event_data["event_name"] : "unknown, ";
    //        $data2Display += "<br />\n";

    //        $data2Display = "file: ".$dt[0]['file'].", ";
            if ($event_data["event_name"]=="event_onError"){
    //            echo "<pre>";
    //            $data2Display = "class: ".$dt[1]['class'].", ";
    //            $data2Display .= "function: ".$dt[1]['function'].", ";
    //            $data2Display .= "event: ".$event_data["event_name"].", ";
    //            $data2Display .= "<br />\n";
    //
    //            echo $data2Display;
    //            var_dump($event_data['args']);
            }
            if ($event_data["event_name"]=="event_onWarning"){
                if (self::$_instance->_parent->getConfig()->pfp->logger->logWarnings){
                    @\error_log($event_data["WARNING"]["warning_msg"]);
                }
            }
            // todo: here check if $event_data["event_name"] is registered from caller eg. already exists in $this->_registered_events
            // if not, just return FALSE;

            if (\array_key_exists("args", $event_data)){
                // here we have 'args', call (run) all registered actions (for this event) providing them the 'args'
    //            echo '<pre>'; echo "<br />\n";
    //            echo "from eventManager, eventData before change:<br />\n";
    //            var_dump($event_data);
    //
    //            $event_data['args'] = self::$_instance->changeValues($event_data['args']);
    //
    //            echo "<br />\n";
    //            echo "from eventManager, eventData after change:<br />\n";
    //            var_dump($event_data);
            } else {
                // here we don't have 'args', just call (run) all registered actions (for this event)

            }
            return \TRUE;
        }

        protected static function changeValues($array){
            // test function for EventActions prototype.

            $arr = $array;
            if (\array_key_exists("a1", $arr)){
                $arr['a1'] = 'some_var_modified';
            }

            if (\array_key_exists("a2", $arr)){
                $arr['a2'] = 'modified_Zeakis';
            }

            if (\array_key_exists("a3", $arr)){
                if (\array_key_exists("array_arg1", $arr['a3'])){
                    $arr['a3']["array_arg1"] = 'test1_modified';
                }
            }
            return $arr;
        }


        public static function getEvents_ofObject($object){

        }

        public static function getEvent_Actions($eventName){

        }

        public static function getSuspendedEvents(){

        }

        public static function getSuspended_Actions($of_eventName){

        }

        public static function suspendEvent($eventName){
            //for how long ???
        }

        public static function suspendAction($of_eventName){
            //for how long ???
        }

        public static function removeEventActions(array $actions){

        }

        public static function removeEventAction($action){

        }

        public static function registerEventAction($event, $action){

        }

        public static function registerAction_forAllEvents_ofObject($action, $object){

        }

        public static function removeAllEventActions($event){

        }

        public static function getNextRunningAction($of_event){

        }

        public static function getPreviousRunningAction($of_event){

        }

        public static function getRunningEvent(){

        }

        /* 
         * we don't permit an explicit call of the constructor! 
         * (like $v = new Singleton()) 
         * 
         */
        protected function __construct(){}

        /* 
         * we don't permit cloning the singleton (like $x = clone $v) 
         * 
         */
        protected function __clone(){}

    }
}
?>
